package com.mmlc.meimei.lichengkongjian.brand;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.BrandListResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.BladeListView;

import java.util.List;

/**
 * Created by Administrator on 2016/7/13.
 */
public class BrandAdapter extends BaseExpandableListAdapter {
    Context context;
    List<BrandListResponseBean.DataBean> datas;
    BladeListView bladeListView;

    private String brandId;
    public BrandAdapter(Context context, List<BrandListResponseBean.DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }
    public BrandAdapter(Context context, List<BrandListResponseBean.DataBean> datas, BladeListView bladeListView) {
        this(context,datas);
        this.bladeListView = bladeListView;

    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_blade_listview_group, parent, false);
        }

        TextView tv_group = ViewUtils.ViewHolder.get(convertView, R.id.tv_group);

        tv_group.setText(datas.get(groupPosition).getCapital());

        convertView.setClickable(true);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_blade_listview_child, parent, false);
        }

        TextView tv_child = ViewUtils.ViewHolder.get(convertView, R.id.tv_child);

        ImageView checkView = (ImageView) convertView.findViewById(R.id.iv_check);
        tv_child.setText(datas.get(groupPosition).getList().get(childPosition).getBrand_name());

        if(TextUtils.equals(brandId,datas.get(groupPosition).getList().get(childPosition).getId())){
            checkView.setVisibility(View.VISIBLE);
        }else{
            checkView.setVisibility(View.GONE);
        }

        return convertView;
    }

    public String getBrandId() {
        return brandId;
    }

    public void setBrandId(String brandId) {
        this.brandId = brandId;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
